
import { _polar, translateY, _random } from "../math/index.js"
export const treeCurveTypes = ['QQ', 'QL', 'V', 'LL']
export const treePoints = (options) => {
    let { o = [300, 200], n = 3, r = 100, a = -90, depth = 1, k = 2 / 3, wriggle = 10, treeCurve = 'QQ' } = options
    let result = new Array(depth)
    let _ra = () => {
        return wriggle ? _random(-wriggle, wriggle) : 0
    }
    let _iter = (o, r, a, depth) => {
        let p = _polar(o, r, a)
        let a1 = a + 30 + _ra(), a2 = a - 30 + _ra()
        let r1 = r * k, r2 = r * k
        let p1 = _polar(p, r1, a1)
        let p2 = _polar(p, r2, a2)
        if (!result[depth]) {
            result[depth] = []
        }
        switch (treeCurve) {
            case 'QL':
                // 曲线1 QL
                result[depth].push([o, [...p, ...p1]], [p, p2])
                break;
            case 'QQ':
                // 曲线2 QQ
                result[depth].push([o, [...p, ...p1]], [o, [...p, ...p2]])
                break;
            case 'V':
                // 二叉树 V
                result[depth].push([o, p1], [o, p2])
                break
            default:
                // 二叉树 LL
                result[depth].push([o, p, p1], [p, p2])
        }

        // 
        // result.push([o, p], [p, p1], [p, p2])
        if (depth > 0) {
            _iter(p1, r1, a1, depth - 1)
            _iter(p2, r2, a2, depth - 1)
        }
    }
    if (n == 1) {
        _iter(translateY(o, r), r, a, depth)
    } else {
        Array.from({ length: n }).forEach((_, i) => {
            _iter(o, r, a + i * 360 / n, depth)
        })
    }

    return result.reverse()
}